unit u_TabelaImposto;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Mask, ToolEdit, CurrEdit, ExtCtrls, Buttons, Db, DBClient;

type
  TfrmTabelaImposto = class(TForm)
    TabelaINSS: TClientDataSet;
    TabelaIRRF: TClientDataSet;
    TabelaINSSVlr1: TFloatField;
    TabelaINSSPerc1: TFloatField;
    TabelaINSSVlr2: TFloatField;
    TabelaINSSPerc2: TFloatField;
    TabelaINSSVlr3: TFloatField;
    TabelaINSSPerc3: TFloatField;
    TabelaINSSVlr4: TFloatField;
    TabelaINSSPerc4: TFloatField;
    TabelaIRRFVlr1: TFloatField;
    TabelaIRRFPerc1: TFloatField;
    TabelaIRRFDed1: TFloatField;
    TabelaIRRFVlr2: TFloatField;
    TabelaIRRFPerc2: TFloatField;
    TabelaIRRFDed2: TFloatField;
    TabelaIRRFVlr3: TFloatField;
    TabelaIRRFPerc3: TFloatField;
    TabelaIRRFDed3: TFloatField;
    Shape1: TShape;
    Panel2: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Panel5: TPanel;
    edVlr_IRRF1: TCurrencyEdit;
    edVlr_IRRF2: TCurrencyEdit;
    edVlr_IRRF3: TCurrencyEdit;
    edPer_IRRF1: TCurrencyEdit;
    edPer_IRRF2: TCurrencyEdit;
    edPer_IRRF3: TCurrencyEdit;
    edDed_IRRF1: TCurrencyEdit;
    edDed_IRRF2: TCurrencyEdit;
    edDed_IRRF3: TCurrencyEdit;
    Panel3: TPanel;
    BitBtn1: TBitBtn;
    Panel6: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Panel7: TPanel;
    edVlr_INSS1: TCurrencyEdit;
    edVlr_INSS2: TCurrencyEdit;
    edVlr_INSS3: TCurrencyEdit;
    edVlr_INSS4: TCurrencyEdit;
    edPer_INSS1: TCurrencyEdit;
    edPer_INSS2: TCurrencyEdit;
    edPer_INSS3: TCurrencyEdit;
    edPer_INSS4: TCurrencyEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure CarregaTabela;
    function RetornaValor(vlr: Double): Double;

    function RetornaValor_INSS(vlr: Double): Double;
    function RetornaValor_IRRF(vlr, inss: Double): Double;

  end;

var
  frmTabelaImposto: TfrmTabelaImposto;
  Arq: TextFile;
  linha: string;
  caminho: string;
  achou: Boolean;

implementation

uses clsParametros, Biblio;

var
  Parametros: TParametros;

{$R *.DFM}

procedure TfrmTabelaImposto.FormCreate(Sender: TObject);
begin
  Parametros := TParametros.Create;
  Parametros.Recuperar('1');
  caminho := Parametros.Repos_personal;
  achou := False;
  if FileExists(caminho + 'TabelaImposto.rpa') then
    CarregaTabela;
end;

procedure TfrmTabelaImposto.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Parametros.Free;
end;

procedure TfrmTabelaImposto.FormActivate(Sender: TObject);
begin
  if achou then begin
    //Tabela de INSS
    edVlr_INSS1.Value := TabelaINSSVlr1.Value;
    edVlr_INSS2.Value := TabelaINSSVlr2.Value;
    edVlr_INSS3.Value := TabelaINSSVlr3.Value;
    edVlr_INSS4.Value := TabelaINSSVlr4.Value;

    edPer_INSS1.Value := TabelaINSSPerc1.Value;
    edPer_INSS2.Value := TabelaINSSPerc2.Value;
    edPer_INSS3.Value := TabelaINSSPerc3.Value;
    edPer_INSS4.Value := TabelaINSSPerc4.Value;

    //Tabela de IRRF
    edVlr_IRRF1.Value := TabelaIRRFVlr1.Value;
    edVlr_IRRF2.Value := TabelaIRRFVlr2.Value;
    edVlr_IRRF3.Value := TabelaIRRFVlr3.Value;

    edPer_IRRF1.Value := TabelaIRRFPerc1.Value;
    edPer_IRRF2.Value := TabelaIRRFPerc2.Value;
    edPer_IRRF3.Value := TabelaIRRFPerc3.Value;

    edDed_IRRF1.Value := TabelaIRRFDed1.Value;
    edDed_IRRF2.Value := TabelaIRRFDed2.Value;
    edDed_IRRF3.Value := TabelaIRRFDed3.Value;
  end;
end;

procedure TfrmTabelaImposto.BitBtn1Click(Sender: TObject);
begin
  AssignFile(Arq, caminho + 'TabelaImposto.rpa');
  Rewrite(Arq);
  //Tabela INSS
  linha := StrZero(iv(edVlr_INSS1.Value * 100), 15) + StrZero(iv(edPer_INSS1.Value * 100), 15);
  Writeln(Arq, linha);
  linha := StrZero(iv(edVlr_INSS2.Value * 100), 15) + StrZero(iv(edPer_INSS2.Value * 100), 15);
  Writeln(Arq, linha);
  linha := StrZero(iv(edVlr_INSS3.Value * 100), 15) + StrZero(iv(edPer_INSS3.Value * 100), 15);
  Writeln(Arq, linha);
  linha := StrZero(iv(edVlr_INSS4.Value * 100), 15) + StrZero(iv(edPer_INSS4.Value * 100), 15);
  Writeln(Arq, linha);

  //Tabela IRRF
  linha := StrZero(iv(edVlr_IRRF1.Value * 100), 15) + StrZero(iv(edPer_IRRF1.Value * 100), 15) + StrZero(iv(edDed_IRRF1.Value * 100), 15);
  Writeln(Arq, linha);
  linha := StrZero(iv(edVlr_IRRF2.Value * 100), 15) + StrZero(iv(edPer_IRRF2.Value * 100), 15) + StrZero(iv(edDed_IRRF2.Value * 100), 15);
  Writeln(Arq, linha);
  linha := StrZero(iv(edVlr_IRRF3.Value * 100), 15) + StrZero(iv(edPer_IRRF3.Value * 100), 15) + StrZero(iv(edDed_IRRF3.Value * 100), 15);
  Writeln(Arq, linha);

  CloseFile(Arq);

  Close;
end;

procedure TfrmTabelaImposto.CarregaTabela;
var
  Valor: Double;
begin
  achou := True;
  AssignFile(Arq, caminho + 'TabelaImposto.rpa');
  Reset(Arq);

  //Tabela de INSS
  TabelaINSS.Close;
  TabelaINSS.CreateDataSet;
  TabelaINSS.Open;

  TabelaINSS.Append;

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaINSSVlr1.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaINSSPerc1.Value := RetornaValor(Valor);

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaINSSVlr2.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaINSSPerc2.Value := RetornaValor(Valor);

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaINSSVlr3.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaINSSPerc3.Value := RetornaValor(Valor);

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaINSSVlr4.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaINSSPerc4.Value := RetornaValor(Valor);

  TabelaINSS.Post;

  //Tabela IRRF
  TabelaIRRF.Close;
  TabelaIRRF.CreateDataSet;
  TabelaIRRF.Open;

  TabelaIRRF.Append;

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaIRRFVlr1.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaIRRFPerc1.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 31, 15));
  TabelaIRRFDed1.Value := RetornaValor(Valor);

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaIRRFVlr2.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaIRRFPerc2.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 31, 15));
  TabelaIRRFDed2.Value := RetornaValor(Valor);

  Readln(Arq, linha);
  Valor := StrToFloat(copy(linha, 01, 15));
  TabelaIRRFVlr3.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 16, 15));
  TabelaIRRFPerc3.Value := RetornaValor(Valor);
  Valor := StrToFloat(copy(linha, 31, 15));
  TabelaIRRFDed3.Value := RetornaValor(Valor);

  TabelaIRRF.Post;

  CloseFile(Arq);
end;

function TfrmTabelaImposto.RetornaValor(vlr: Double): Double;
begin
  if vlr <> 0 then
    Result := vlr / 100
  else
    Result := 0;
end;

function TfrmTabelaImposto.RetornaValor_INSS(vlr: Double): Double;
var
  Valor: Double;
begin
  Valor := 0;

  if (vlr >= 0) and (vlr <= TabelaINSSVlr1.Value) then
    Valor := vlr * (TabelaINSSPerc1.Value / 100);

  if (vlr > TabelaINSSVlr1.Value) and (vlr <= TabelaINSSVlr2.Value) then
    Valor := vlr * (TabelaINSSPerc2.Value / 100);

  if (vlr > TabelaINSSVlr2.Value) and (vlr <= TabelaINSSVlr3.Value) then
    Valor := vlr * (TabelaINSSPerc3.Value / 100);

  if (vlr > TabelaINSSVlr3.Value) then
    Valor := vlr * (TabelaINSSPerc4.Value / 100);

  Result := Valor;
  if Valor > (TabelaINSSVlr4.Value * (TabelaINSSPerc4.Value / 100)) then
    Result := (TabelaINSSVlr4.Value * (TabelaINSSPerc4.Value / 100));
end;

function TfrmTabelaImposto.RetornaValor_IRRF(vlr, inss: Double): Double;
var
  Bc, Valor: Double;
begin
  Valor := 0;
  Bc := vlr - inss;

  if (Bc <= TabelaIRRFVlr1.Value) then
    Valor := 0;

  if (Bc > TabelaIRRFVlr1.Value) and (Bc <= TabelaIRRFVlr2.Value) then
    Valor := (Bc * (TabelaIRRFPerc2.Value / 100)) - TabelaIRRFDed2.Value;

  if (Bc > TabelaIRRFVlr2.Value) then
    Valor := (Bc * (TabelaIRRFPerc3.Value / 100)) - TabelaIRRFDed3.Value;

  Result := Valor;
end;

end.
